interface Handler {
    setNext(handler: Handler): Handler
    handle(request: string | null): string | null
}

abstract class AbstractHandler implements Handler {
    //下一个对象
    private nextHandler: Handler | null = null

    setNext(handler: Handler): Handler {
        this.nextHandler = handler
        return handler
    }

    handle(request: string | null): string | null {
        //存在下一个需要处理的对象则进行相应操作
        if (this.nextHandler) {
            return this.nextHandler.handle(request)
        }
        return null
    }
}

class _900PushHandler extends AbstractHandler {

    handle(request: string | null): string | null {
        if (request === '900') {
            return `900详情推送处理器：这个推送我处理了，推送号段：${request}`
        }
        return super.handle(request);
    }
}


class _3000PushHandler extends AbstractHandler {

    handle(request: string | null): string | null {
        if (request === '3000') {
            return `3000第三方任务推送处理器：这个推送我处理了，推送号段：${request}`
        }
        return super.handle(request);
    }
}


class _3001PushHandler extends AbstractHandler {
    handle(request: string | null): string | null {
        if (request === '3001') {
            return `3001抢单任务推送处理器：这个推送我处理了，推送号段：${request}`
        }
        return super.handle(request);
    }
}


const ChainOfResponsibilityTest = (handler: Handler) => {
    //模拟推送服务发送消息
    const pushCodes = ['3000', '900', '700'];
    for (const pushCode of pushCodes) {
        console.log(`发送一个推送消息，号段是： ${pushCode}`);
        const result = handler.handle(pushCode);

        if (result) {
            console.log(` 推送消息给处理了 ${result}`);
        } else {
            console.log(` 这个推送号段没有人处理： ${pushCode}`);
        }
    }
}


const _m900PushHandler = new _900PushHandler();
const _m3000PushHandler = new _3000PushHandler();
const _m3001PushHandler = new _3001PushHandler();
//责任链模式：链接处理器
_m900PushHandler.setNext(_m3000PushHandler).setNext(_m3001PushHandler);
ChainOfResponsibilityTest(_m900PushHandler)



